package com.kg.sdk.dlg;

import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.support.annotation.NonNull;
import android.view.Display;
import android.view.LayoutInflater;
import android.view.View;
import android.view.WindowManager;
import android.widget.TextView;

import com.kg.sdk.activity.KPChargeActivity;
import com.kg.sdk.activity.KPUserCenterActivity;
import com.kg.sdk.util.Helper;
import com.kg.sdk.util.KPNetworkData;

import org.w3c.dom.Text;

public class KPChargeDlg extends Dialog implements View.OnClickListener  {
    private OKClickListener listener;
    private TextView tvUseKB;
    private TextView btnOk;
    private TextView tvChargeKB;
    private TextView tvChargeName;
    private TextView tvChargeKB1;
    private TextView tvPay;
    private View frClose;
    private String account;
    private String useKB;
    private String surplus;
    private WaitingDialog waitingDialog;
    public KPChargeDlg(@NonNull Context context, OKClickListener listener) {
        super(context, Helper.getStyleResId(context,"DGDialog_NoTitle_Transparent"));
        this.listener = listener;
        initView();
        waitingDialog = new WaitingDialog(getContext());
        waitingDialog.setWaitingText("充值中...");
    }
    protected void initDialog(){
        WindowManager windowManager = this.getWindow().getWindowManager();
        Display display = windowManager.getDefaultDisplay();
        WindowManager.LayoutParams lp = getWindow().getAttributes();
        lp.width = (int)(display.getWidth() * 0.8); //设置宽度
        lp.height =  WindowManager.LayoutParams.WRAP_CONTENT ;
        getWindow().setAttributes(lp);
    }
    protected void initView(){
        View  mMainView = getMainView();
        setContentView(mMainView);
    }

    public void setUseKB(String useKB){
        tvUseKB.setText(useKB);
        this.useKB = useKB;
    }

    public void setAccount(String account){
        this.account = account;
    }
    public void setChargeKB(String chargeKB){
        tvChargeKB.setText(chargeKB);
        tvChargeKB1.setText(chargeKB);
    }

    public void setChargeName(String chargeName){
        tvChargeName.setText(chargeName);
    }
    protected View getMainView() {
        int layoutId = Helper.getLayoutResId(getContext(),"dlg_pay");
        View view = LayoutInflater.from(getContext()).inflate(layoutId,null);
        tvUseKB = (TextView)view.findViewById(Helper.getIdResId(getContext(),"tvUseKB"));
        tvChargeKB = (TextView) view.findViewById(Helper.getIdResId(getContext(),"tvChargeKB"));
        tvChargeKB1 = (TextView) view.findViewById(Helper.getIdResId(getContext(),"tvChargeKB1"));
        tvChargeName = (TextView) view.findViewById(Helper.getIdResId(getContext(),"tvChargeName"));
        btnOk =  (TextView)view.findViewById(Helper.getIdResId(getContext(),"tvCharge"));
        tvPay = (TextView) view.findViewById(Helper.getIdResId(getContext(),"tvPay"));
        frClose = view.findViewById(Helper.getIdResId(getContext(),"frClose"));
       // btnCancel.setOnClickListener(this);
        tvPay.setOnClickListener(this);
        btnOk.setOnClickListener(this);
        frClose.setOnClickListener(this);
       // setTip("是否进行酷币支付?");
        return view;
    }

    public void setTip(String tip){
      //  tvTip.setText(tip);
    }
    private void startCharge(){
        Intent intent = new Intent();
        intent.putExtra(KPNetworkData.ACCOUNT,account);
        intent.putExtra(KPNetworkData.SURPLUS,useKB);
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        intent.setClass(getContext(), KPChargeActivity.class);
        getContext().startActivity(intent);
    }
    @Override
    public void onClick(View v) {
        int id = v.getId();
        if(id == Helper.getIdResId(getContext(),"tvPay")){
            onOkClick();
            waitingDialog.showDialog();
        }else if(id == Helper.getIdResId(getContext(),"tvCharge")){
            startCharge();
        }else if(id == Helper.getIdResId(getContext(),"frClose")){
            dismiss();
        }

    }

    public void dismiss(){
        super.dismiss();
        waitingDialog.closeDialog();
    }
    public void onOkClick(){
        if(listener != null){
            listener.okClick();
        }
    }

    public void showDialog(){
        if(!isShowing()){
            show();
        }
    }


    public interface OKClickListener{
        public void okClick();
    }

    public void closeWaitDlg(){
        waitingDialog.closeDialog();
    }
}
